package edu.scau.web.sku;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.scau.core.constants.AjaxResult;
import edu.scau.core.constants.HttpStatus;
import edu.scau.sku.domain.SkuColorSize;
import edu.scau.sku.service.SkuColorSizeService;
import edu.scau.sku.vo.SkuColorSizeVo;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存详情 Controller
 */
@Tag(name = "库存详情管理(废弃)", description = "库存详情接口")
@RestController
@Hidden
@RequestMapping("/skuColorSize")
public class SkuColorSizeController {

    @Autowired
    private SkuColorSizeService skuColorSizeService;

    @Operation(
        summary = "查找所有",
        description = "查找所有未删除的库存详情信息, 访问权限: ALL",
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SkuColorSizeVo.class))),
            @ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @GetMapping("/listAll")
    public AjaxResult listAll() {
        List<SkuColorSizeVo> skuColorSizeList = skuColorSizeService.listAll();
        return AjaxResult.success(skuColorSizeList);
    }

    @Operation(
        summary = "分页查找",
        description = "分页查找所有未删除的库存详情信息, 访问权限: ALL",
        parameters = {
            @Parameter(name = "pageNum", description = "页码", required = true, schema = @Schema(type = "int")),
            @Parameter(name = "pageSize", description = "每页数据条数", required = true, schema = @Schema(type = "int")),
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SkuColorSizeVo.class))), // 返回 PageInfo 对象
            @ApiResponse(responseCode = "500", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))) // 返回 AjaxResult 对象
        }
    )
    @GetMapping("/listByPage")
    public AjaxResult listByPage(
        @RequestParam("pageNum") Integer pageNum,
        @RequestParam("pageSize") Integer pageSize,
        SkuColorSizeVo skuColorSizeVo
    ) {
        PageHelper.startPage(pageNum, pageSize);
        List<SkuColorSizeVo> skuColorSizeList = skuColorSizeService.listByPage(skuColorSizeVo);
        PageInfo<SkuColorSizeVo> pageInfo = new PageInfo<>(skuColorSizeList);
        return AjaxResult.success(pageInfo);
    }

    @Operation(
        summary = "根据code查找库存详情",
        description = "根据ID查找库存详情信息, 访问权限: ALL",
        parameters = {
            @Parameter(name = "skuCode", description = "库存详情编号", required = true, schema = @Schema(type = "String"))
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = SkuColorSize.class))),
            @ApiResponse(responseCode = "204", description = "不存在", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @GetMapping("/findSkuColorSizeById")
    public AjaxResult findSkuColorSizeByCode(@RequestParam("skuCode") String skuCode) {
        SkuColorSizeVo skuColorSize = skuColorSizeService.findSkuColorSizeByCode(skuCode);
        if (skuColorSize != null) {
            return AjaxResult.success(skuColorSize);
        } else {
            return AjaxResult.success("资源不存在！", HttpStatus.NO_CONTENT);
        }
    }

    @Operation(
        summary = "更新库存详情",
        description = "根据Code更新信息, skuCode必需字段, 访问权限: admin 、 IM",
        requestBody = @RequestBody(
            description = "更新条件,code是必须的",
            required = true,
            content = @Content(
                mediaType = "application/json",
                schema = @Schema(implementation = SkuColorSizeVo.class)
            )
        ),
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "更新失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin', 'IM')")
    public AjaxResult updateSkuColorSize(@org.springframework.web.bind.annotation.RequestBody SkuColorSizeVo skuColorSizeVo) {
         try {
             int res = skuColorSizeService.updateSkuColorSize(skuColorSizeVo);
             return res > 0 ? AjaxResult.success(HttpStatus.SUCCESS) : AjaxResult.error("更新失败");
         }
         catch (RuntimeException e){
             return AjaxResult.error(HttpStatus.BAD_REQUEST,e.getMessage());
         }
    }

    @Operation(
        summary = "插入库存信息",
        description = "插入库存详情信息, skuCode必需字段, 访问权限: admin、IM",
        requestBody = @RequestBody(
            description = "插入库存详情条件",
            required = true,
            content = @Content(
                mediaType = "application/json",
                schema = @Schema(implementation = SkuColorSizeVo.class)
            )
        ),
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "插入失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @PutMapping("/insert")
    @PreAuthorize("hasAnyAuthority('admin', 'IM')")
    public AjaxResult insertSkuColorSize(@org.springframework.web.bind.annotation.RequestBody SkuColorSizeVo skuColorSizeVo) {
        try {
            int res = skuColorSizeService.insertSkuColorSize(skuColorSizeVo);
            return res > 0 ? AjaxResult.success(200) : AjaxResult.error("插入失败！");
        }
        catch (IllegalArgumentException | DataIntegrityViolationException e){
            return AjaxResult.error(HttpStatus.BAD_REQUEST,e.getMessage());
        }
    }

    @Operation(
        summary = "删除库存详情",
        description = "根据skuCode删除库存详情信息, 访问权限: admin、IM",
        parameters = {
            @Parameter(name = "skuCode", description = "skuCode", required = true, schema = @Schema(type = "String"))
        },
        responses = {
            @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class))),
            @ApiResponse(responseCode = "500", description = "删除失败", content = @Content(mediaType = "application/json", schema = @Schema(implementation = HttpStatus.class)))
        }
    )
    @DeleteMapping("/delete")
    @PreAuthorize("hasAnyAuthority('admin', 'IM')")
    public AjaxResult delete(@RequestParam("skuCode") String skuCode) {
        int res = skuColorSizeService.deleteSkuColorSizeByCode(skuCode);
        return res > 0 ? AjaxResult.success("删除成功", 200) : AjaxResult.error("删除失败");
    }
}
